﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ACLic
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();

            this.Shown += new EventHandler(FrmMain_Shown);
            this.txtKey1.TextChanged += new EventHandler(txtKey_TextChanged);
            this.txtKey2.TextChanged += new EventHandler(txtKey_TextChanged);
            this.txtKey3.TextChanged += new EventHandler(txtKey_TextChanged);
            this.txtKey4.TextChanged += new EventHandler(txtKey_TextChanged);

            this.btnCreate.Click += new EventHandler(btnCreate_Click);
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            string skey = this.txtKey1.Text + this.txtKey2.Text + this.txtKey3.Text + this.txtKey4.Text;
            this.txtCode.Text = CreateActiceCode(skey);
        }

        private void txtKey_TextChanged(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text.Length == 4)
            {
                if (txt == this.txtKey1)
                    this.txtKey2.Focus();
                else
                    if (txt == this.txtKey2)
                        this.txtKey3.Focus();
                    else
                        if (txt == this.txtKey3)
                            this.txtKey4.Focus();
            }
        }

        private void FrmMain_Shown(object sender, EventArgs e)
        {
            this.txtKey1.Focus();
        }

        public string CreateActiceCode(string skey)
        {
            string returnValue = "XXXXXXXXXXXXXXXX";
            int i = 0;
            string key = "";
            for (i = 0; i < skey.Length; i++)
                if (skey[i] >= '0' && skey[i] <= '9')
                    key += skey[i].ToString();

            if (key.Length != 16)
                return returnValue;

            if (key != "")
            {
                byte[] arrByte = Encoding.UTF8.GetBytes(key);
                int length = arrByte.Length;
                long lvalue = 0;
                bool flag = true;

                while (lvalue < 10000000000000000)
                {
                    for (i = 0; i < length; i++)
                    {
                        if (arrByte[i] > 0)
                        {
                            if (flag)
                                lvalue += Convert.ToInt64(arrByte[i]);
                            else
                                lvalue *= Convert.ToInt64(arrByte[i]);

                            if (lvalue >= 10000000000000000 && lvalue >= 0)
                                break;

                            flag = !flag;
                        }
                    }
                }

                string str = lvalue.ToString().Substring(0, 16);
                str = str.Substring(0, 4) + " - " + str.Substring(4, 4) + " - " + str.Substring(8, 4) + " - " + str.Substring(12, 4);
                returnValue = str;
            }

            return returnValue;
        }
    }
}
